package com.nobleoxtechnology.weatherSMS;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MessageParsing implements View.OnClickListener
{
	private Context context;
	private SharedPreferences preferences;
	private ArrayList<Spinner> splitLine = new ArrayList<Spinner>();
	private ArrayList<Spinner> delimiter = new ArrayList<Spinner>();
	private ArrayList<Spinner> delimNumber = new ArrayList<Spinner>();
	EditText editPhone, editSMS;

	private String splitLineOption[] = { "spinnerTempLine" };
	private String delimiterOption[] = { "spinnerTempDelim" };
	private String delimNumberOption[] = { "spinnerTempDelimNum" };

	private ArrayList<Integer> splitLineDefault = new ArrayList<Integer>();
	private ArrayList<Integer> delimiterDefault = new ArrayList<Integer>();
	private ArrayList<Integer> delimNumberDefault = new ArrayList<Integer>();

	public MessageParsing(Context context, Activity activity, SharedPreferences preferences)
	{
		this.context = context;
		this.preferences = preferences;
		
		ArrayList<Integer> splitLineArray = new ArrayList<Integer>();
		ArrayList<Integer> splitDelimiter = new ArrayList<Integer>();
		ArrayList<Integer> splitDelimiterNumber = new ArrayList<Integer>();

		splitLineDefault.add(preferences.getInt("spinnerTempLine", 2));
		delimiterDefault.add(preferences.getInt("spinnerTempDelimiter", 0));
		delimNumberDefault.add(preferences.getInt("spinnerTempDelimNum", 0));

		for (int i = 0; i < splitLineOption.length; i++)
		{
			splitLineArray.add(preferences.getInt(splitLineOption[i], splitLineDefault.get(i)));
		}
		
		for (int i = 0; i < delimiterOption.length; i++)
		{
			splitDelimiter.add(preferences.getInt(delimiterOption[i], delimiterDefault.get(i)));
		}
		
		for (int i = 0; i < delimNumberOption.length; i++)
		{
			splitDelimiterNumber.add(preferences.getInt(delimNumberOption[i], delimNumberDefault.get(i)));
		}
		
		splitLine.add((Spinner) activity.findViewById(R.id.spinnerTempLine));
		
		delimiter.add((Spinner) activity.findViewById(R.id.spinnerTempDelim));
		
		delimNumber.add((Spinner) activity.findViewById(R.id.spinnerTempDelimNum));
		
		for (int i = 0; i < splitLine.size(); i++)
		{
			splitLine.get(i).setAdapter(ArrayAdapter.createFromResource(context, 
					R.array.split_number, 
					android.R.layout.simple_spinner_item));
			splitLine.get(i).setSelection(splitLineArray.get(i));
			
			delimiter.get(i).setAdapter(ArrayAdapter.createFromResource(context, 
					R.array.split_delimiter, 
					android.R.layout.simple_spinner_item));
			delimiter.get(i).setSelection(splitDelimiter.get(i));
			
			delimNumber.get(i).setAdapter(ArrayAdapter.createFromResource(context, 
					R.array.split_number, 
					android.R.layout.simple_spinner_item));
			delimNumber.get(i).setSelection(splitDelimiterNumber.get(i));
		}
		
		editPhone = (EditText) activity.findViewById(R.id.editPhoneNumber);
		editSMS = (EditText) activity.findViewById(R.id.editSend);
        
		editPhone.setText(preferences.getString("phone", "466453"));
		editSMS.setText(preferences.getString("send", "%zip% weather"));
	}
	
	@Override
	public void onClick(View v)
	{
		Editor weatherEditor = preferences.edit();
		// Save using code similar to this:
		// Replace *Default with actual
		
		// Use error handling for this
		for (int i = 0; i < splitLineOption.length; i++)
		{
			weatherEditor.putInt(splitLineOption[i], splitLine.get(i).getSelectedItemPosition());
		}
		
		for (int i = 0; i < delimiterOption.length; i++)
		{
			weatherEditor.putInt(delimiterOption[i], delimiter.get(i).getSelectedItemPosition());
		}
		
		for (int i = 0; i < delimNumberOption.length; i++)
		{
			weatherEditor.putInt(delimNumberOption[i], delimNumber.get(i).getSelectedItemPosition());
		}

        weatherEditor.putString("phone", editPhone.getText().toString());
        weatherEditor.putString("send", editSMS.getText().toString());
        
		weatherEditor.commit();
		Toast.makeText(context, "Settings saved successfully!", Toast.LENGTH_LONG).show();
	}
}
